package judesart.app.server;

import gwtupload.server.exceptions.UploadActionException;
import gwtupload.server.gae.BlobstoreUploadAction;
import gwtupload.server.gae.BlobstoreFileItemFactory.BlobstoreFileItem;

import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PhotosUploaderServlet extends BlobstoreUploadAction {
    private static final long serialVersionUID = 1L;

    private final static Log LOGGER = LogFactory.getLog(PhotosUploaderServlet.class);

    @SuppressWarnings("unchecked")
    @Override
    public String executeAction(HttpServletRequest request,
            java.util.List<FileItem> sessionFiles) throws UploadActionException {
        LOGGER.debug("*********************************************");
        for (FileItem fileItem : sessionFiles) {
            LOGGER.debug(" --> " + fileItem.getFieldName());
        }
        List<BlobstoreFileItem> _sessionFiles = (List) sessionFiles;
        List<String> list = (List<String>) request.getSession().getAttribute(
                "JAW-UPLOADED-FILES");
        if (list == null) {
            list = new Vector<String>();
            request.getSession().setAttribute("JAW-UPLOADED-FILES", list);
        }
        for (BlobstoreFileItem blobstoreFileItem : _sessionFiles) {
            list.add(blobstoreFileItem.getKey().getKeyString());
        }
        LOGGER.debug("Image saved to session");
        return null;
    }
}
